package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysOption;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysRecord;
import com.ruoyi.project.system.service.ISysRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author theme
 * @date 2020-05-27
 */
@Api(description = "报名记录" )
@RestController
@RequestMapping("/system/record" )
public class SysRecordController extends BaseController {
    @Autowired
    private ISysRecordService sysRecordService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysOptionService sysOptionService;

/**
 * 查询【请填写功能名称】列表
 */
    @ApiOperation("查询【请填写功能名称】列表" )
    @PreAuthorize("@ss.hasPermi('system:record:list')" )
    @GetMapping("/list" )
        public TableDataInfo list(SysRecord sysRecord) {
        startPage();
        List<SysRecord> list = sysRecordService.selectSysRecordList(sysRecord);
        return getDataTable(list);
    }

    @ApiOperation("查询填写过信息的列表" )
    @PreAuthorize("@ss.hasPermi('system:record:record')" )
    @GetMapping("/recordListMobileUser" )
    public TableDataInfo record() {
        startPage();
        List<SysRecord> sysRecords = sysRecordService.selectSysRecordListMobileUser();
        List<SysMobileUser> list = new ArrayList<>();
        for (int i = 0; i < sysRecords.size(); i++) {
            SysRecord sysRecord = sysRecords.get(i);
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysRecord.getMobileUserId());
            if (null != sysMobileUser) {
                Long mobileUserId = sysMobileUser.getMobileUserId();
                List<SysRecord> records = sysRecordService.selectSysRecordByMobileUserId(mobileUserId);
                List<SysRecord> recordList = new ArrayList<>();
                for (int j = 0; j < records.size(); j++) {
                    Long optionId = records.get(j).getOptionId();
                    SysOption sysOption = sysOptionService.selectSysOptionById(optionId);
                    records.get(j).setSysOption(sysOption);
                    recordList.add(records.get(j));
                }
                sysMobileUser.setRecords(recordList);
                list.add(sysMobileUser);
            }
        }
        return getDataTable(list);

    }

    
    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation("获取【请填写功能名称】详细信息" )
    @PreAuthorize("@ss.hasPermi('system:record:query')" )
    @GetMapping(value = "/{mobileUserId}" )
    public AjaxResult getInfo(@PathVariable("mobileUserId" ) Long mobileUserId) {
        return AjaxResult.success(sysRecordService.selectSysRecordById(mobileUserId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("新增【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:record:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRecord sysRecord) {
        return toAjax(sysRecordService.insertSysRecord(sysRecord));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation("修改【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:record:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRecord sysRecord) {
        return toAjax(sysRecordService.updateSysRecord(sysRecord));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation("删除【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:record:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{mobileUserIds}" )
    public AjaxResult remove(@PathVariable Long[] mobileUserIds) {
        return toAjax(sysRecordService.deleteSysRecordByIds(mobileUserIds));
    }
}
